<?php


namespace app\common\tencentcloud;

class Sms
{
	public $cred;
	public $httpProfile;
	public $clientProfile;
	public $appId;
	public $signName;
	public $config;
	public function __construct($config = [])
	{
		$cred = new \TencentCloud\Common\Credential($config["SecretId"], $config["SecretKey"]);
		$httpProfile = new \TencentCloud\Common\Profile\HttpProfile();
		$httpProfile->setReqMethod("POST");
		$httpProfile->setEndpoint("sms.tencentcloudapi.com");
		$clientProfile = new \TencentCloud\Common\Profile\ClientProfile();
		$clientProfile->setSignMethod("TC3-HMAC-SHA256");
		$clientProfile->setHttpProfile($httpProfile);
		if (!$config) {
			$config = \app\common\server\ConfigServer::get("sms", "tencentcloud");
		}
		$this->cred = $cred;
		$this->httpProfile = $httpProfile;
		$this->clientProfile = $clientProfile;
		$this->appId = $config["appId"];
		$this->signName = $config["signName"];
		$this->config = $config;
	}
	public function send($phone, $templateCode, $data)
	{
		try {
			$client = new \TencentCloud\Sms\V20210111\SmsClient($this->cred, "ap-guangzhou", $this->clientProfile);
			$req = new \TencentCloud\Sms\V20210111\Models\SendSmsRequest();
			$req->SmsSdkAppId = $this->appId;
			$req->SignName = $this->signName;
			$req->TemplateId = $templateCode;
			$req->TemplateParamSet = $data;
			$req->PhoneNumberSet = ["+86" . $phone];
			$req->SessionContext = "";
			$req->ExtendCode = "";
			$req->SenderId = "";
			$resp = $client->SendSms($req);
			$result = json_decode($resp->toJsonString(), true);
			if (isset($result["SendStatusSet"][0]["Code"]) && $result["SendStatusSet"][0]["Code"] == "Ok") {
				return true;
			} else {
				return $result["SendStatusSet"][0]["Message"];
			}
		} catch (\TencentCloud\Common\Exception\TencentCloudSDKException $e) {
			return $e;
		}
	}
	public function code($phone, $code)
	{
		return $this->send($phone, $this->config["template_code"]["verification_code"], [\strval($code)]);
	}
	public function card_buy_deliver($phone, $order_no, $card)
	{
		return $this->send($phone, $this->config["template_code"]["card_buy_deliver"], [\strval($order_no), \strval($card)]);
	}
}